//
//  ProjectTopTabBar.m
//  lcf-newproject
//
//  Created by yangyang on 14-11-25.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "ProjectTopTabBar.h"

@interface ProjectTopTabBar ()

@property (nonatomic,strong)UIImageView * bottomImageView;
@property (nonatomic,strong)NSArray * tabBarNameArray;
@property (nonatomic,assign)int account;

@end

@implementation ProjectTopTabBar

-(instancetype)initWithFrame:(CGRect)frame withTabBarDict:(NSDictionary *)dict{
    self = [super initWithFrame:frame];
    if (self) {
        _tabBarNameArray = [dict objectForKey:@"tabBarName"];
        _account = (int)_tabBarNameArray.count;
    }
    return [self initWithFrame:frame];
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTopTabBar:_tabBarNameArray];
    }
    return self;
}

-(void)createTopTabBar:(NSArray *)nameArray{
    
    
    for (int i = 0; i<_account; i++) {
        if (i!=0) {
            UIImageView * separeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(ProjectTabBarWidth(_account)*i, (ProjectTabBarHeight-14)/2, 1, 14)];
            separeImageView.image = [UIImage imageNamed:@"line01.png"];
            [self addSubview:separeImageView];
        }
        
        UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(ProjectTabBarWidth(_account) * i, 0, ProjectTabBarWidth(3), ProjectTabBarHeight)];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont fontWithName:nil size:13];
        [btn setTitle:[nameArray objectAtIndex:i] forState:UIControlStateNormal];
        if (i == 0) {
            btn.selected = YES;
            _bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(btn.frame.origin.x, ProjectTabBarHeight-3, ProjectTabBarWidth(_account), 3)];
            _bottomImageView.image = [UIImage imageNamed:@"xuanze.png"];
            [self addSubview:_bottomImageView];
        }
        btn.tag = LCFProjectTabBarTag+i;
        [self addSubview:btn];
        [btn addTarget:self action:@selector(tabBarClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

//更换button的selected状态
-(void)tabBarClick:(UIButton *)btn{
    
    [UIView animateWithDuration:0.3 animations:^{
        _bottomImageView.frame = CGRectMake(btn.frame.origin.x, ProjectTabBarHeight-3, ProjectTabBarWidth(_account), 3);
    }];
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton * currentBtn = (UIButton *)view;
            if (currentBtn.tag == btn.tag) {
                currentBtn.selected = YES;
            }else{
                currentBtn.selected = NO;
            }
        }
    }
    
    //NSInteger index = btn.tag-LCFProjectTabBarTag;
    if ([self.delegate respondsToSelector:@selector(customTabBar:didSelectedIndex:)]) {
        [self.delegate customTabBar:self didSelectedIndex:btn.tag];
    }
}





@end
